using System;
using System.Data;
using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Reflection;

namespace PHSRAG.Insight21.ScriptEngine
{
	/// <summary>
	/// Summary description for ScriptReference.
	/// </summary>
	public class ScriptReference
	{
		private DataRow dataRow;

		/// <summary>
		/// Public constructor takes a dataRow, since the Script Loads its references.
		/// </summary>
		/// <param name="row">the row containing the data for the Script Reference</param>
		public ScriptReference( DataRow row )
		{
			dataRow = row;
		}

		/// <summary>
		/// Name of the scriptreference, shortened name since the path can be so long.
		/// </summary>
		public string Name
		{
			get {
				object o = dataRow["Name"];
				return (o == DBNull.Value) ? "" : (string)o; }
		}

		/// <summary>
		/// Full path to the DLL on the Web server being referenced here. Loaded from he ScriptReference table.
		/// </summary>
		public string Path
		{
			get 
			{
				DirectoryInfo directoryInfo = new DirectoryInfo(System.IO.Path.GetDirectoryName(
					(new Regex(@"^file:/+")).Replace(Assembly.GetExecutingAssembly().CodeBase, string.Empty)));
				return directoryInfo.FullName + "\\" + this.Name;
			}
		}

		/// <summary>
		/// GetAllScriptReferences returns all the ScriptReference objects needed to run scripts.
		/// </summary>
		/// <param name="connectionString">database connection string</param>
		/// <returns>returns an arraylist. If there are none, the list will have no entries.</returns>
		public static ArrayList GetAllScriptReferences ( string connectionString )
		{
			ArrayList references = new ArrayList();

			DataSet scriptReferences = Utility.DataAccess.GetDataSet( connectionString,
				"GetAllScriptReferences");

			// create a new Reference for each.
			DataTable table = scriptReferences.Tables[0];

			for( int i=0; i < table.Rows.Count; ++i)
			{
				DataRow row = table.Rows[i];
				ScriptReference reference = new ScriptReference( row );
				references.Add( reference );
			}
		
			// return the array list of references.
			return references;
		}
	}
}
